<?php
namespace app\admin\controller;
class Featured extends  Base
{

    public function index() {
        // 获取推荐位类别
        $types = config('featured.featured_type');
        $type = input('get.type', 0 ,'intval');
        // 获取列表数据
        $results = model("Featured")->getFeaturedsByTypeStatus($type,1);

        return $this->fetch('', [
            'types' => $types,
            'results' => $results,

        ]);
    }
    public function add() {
        if(request()->isPost()) {
            // 入库的逻辑
            $data = input('post.');
            $validate=validate('Featured');
            if (!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $data['status']=1;
            if (!empty($data['id'])){
                return $this->update($data);
            }
            $id = model('Featured')->save($data);
            if($id) {
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else {
            // 获取推荐位类别
            $types = config('featured.featured_type');
            return $this->fetch('', [
                'types' => $types,
            ]);
        }
    }

    public function edit($id)
    {
        $types = config('featured.featured_type');
        $featured=model('Featured')->get($id);
        return $this->fetch('',[
            'types' => $types,
            'featured'=>$featured
        ]);
    }

    public function update($data)
    {
        $id = model('Featured')->save($data,['id'=>$data['id']]);
        if($id) {
            $this->success('更新成功',url('featured/index'));
        }else{
            $this->error('更新失败');
        }
    }

    public function delist() {
        // 获取推荐位类别
        $types = config('featured.featured_type');
        $type = input('get.type', 0 ,'intval');
        // 获取列表数据
        $results = model("Featured")->getFeaturedsByTypeStatus($type,-1);

        return $this->fetch('', [
            'types' => $types,
            'results' => $results,

        ]);
    }

}
